package com.bajiao.app.controller.order;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bajiao.app.base.AppBaseController;
import com.bajiao.common.api.vo.RestResult;
import com.bajiao.common.constant._Whole;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.util.FastJsonHelper;
import com.bajiao.common.util.PasswordUtil;
import com.bajiao.common.util.oConvertUtils;
import com.bajiao.common.web.entity.TableDataInfo;
import com.bajiao.entity.order.CouponOrder;
import com.bajiao.entity.user.User;
import com.bajiao.service.order.CouponOrderService;
import com.bajiao.service.user.UserService;
import com.bajiao.vo.order.ConfirmCouponOrderResponse;
import com.bajiao.vo.order.CouponOrderDetailsResponse;
import com.bajiao.vo.order.CouponOrderResponse;
import com.bajiao.vo.order.OrderDetailsResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = "优惠券订单接口")
@RestController
@RequestMapping("/api/v1/order/coupon")
public class RestCouponOrderController extends AppBaseController {

    @Resource
    private CouponOrderService couponOrderService;

    @PostMapping(value = "/confirm")
    @ApiOperation(value = "确认订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "String", required = true)
    })
    public RestResult<ConfirmCouponOrderResponse> confirm(@ModelAttribute("userId") String userId,
                                                          @RequestParam("couponId") String couponId) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        if (StrUtil.isBlank(couponId))
            throw new DataHandleException("优惠券不存在");
        CouponOrder order = couponOrderService.confirm(userId, couponId);
        ConfirmCouponOrderResponse response = FastJsonHelper.beanToBean(order, ConfirmCouponOrderResponse.class);
        return success(response);
    }

    @PostMapping(value = "/to/pay")
    @ApiOperation(value = "支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "String", required = true),
            @ApiImplicitParam(name = "remark", value = "订单备注", dataType = "String")
    })
    public RestResult toPay(@ModelAttribute("userId") String userId,
                            @RequestParam("couponId") String couponId,
                            @RequestParam(value = "remark", required = false) String remark) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        if (StrUtil.isBlank(couponId))
            throw new DataHandleException("优惠券不存在");
        String orderNo = couponOrderService.toPay(userId, couponId, remark);
        return success("", orderNo);
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "订单分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "status", value = "99-全部 0-待支付 1-待使用 2-已使用 7-售后 9-已取消", defaultValue = "99", dataType = "Integer", required = true),
    })
    public RestResult<TableDataInfo<CouponOrderResponse>> getOrderPage(@ModelAttribute("userId") String userId,
                                                                        @RequestParam(value = "status", defaultValue = "99") Integer status) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        startPage();
        List<CouponOrderResponse> orderPage = couponOrderService.getOrderPage(userId, status);
        return success(getDataTable(orderPage));
    }

    @PostMapping(value = "/cancel")
    @ApiOperation(value = "取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo", value = "订单编号", dataType = "String", required = true)
    })
    public RestResult cancel(@ModelAttribute("userId") String userId,
                             @RequestParam("orderNo") String orderNo) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        couponOrderService.cancel(userId, orderNo);
        return success();
    }

    @PostMapping(value = "/ask/refund")
    @ApiOperation(value = "申请退款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo", value = "订单编号", dataType = "String", required = true),
            @ApiImplicitParam(name = "reason", value = "退款原因", dataType = "Integer"),
    })
    public RestResult askRefund(@ModelAttribute("userId") String userId,
                                @RequestParam("orderNo") String orderNo,
                                @RequestParam(value = "reason", required = false) String reason) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        couponOrderService.askRefund(userId, orderNo, reason);
        return success("", orderNo);
    }

    @PostMapping(value = "/details")
    @ApiOperation(value = "订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo", value = "订单编号", dataType = "String", required = true)
    })
    public RestResult<CouponOrderDetailsResponse> getDetails(@ModelAttribute("user") User user,
                                                       @RequestParam("orderNo") String orderNo) {
        if (ObjectUtil.isNull(user))
            return tokenOverdue();
        CouponOrderDetailsResponse response = couponOrderService.getDetails(user.getId(), orderNo);
        response.setNickname(user.getNickname());
        return success(response);
    }

//    @GetMapping(value = "/use")
//    @ApiOperation(value = "立即使用", notes = "跳转到优惠券订单详情页面")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "String", required = true)
//    })
//    public RestResult<OrderDetailsResponse> getUseIndex(@ModelAttribute("user") User user,
//                                                        @RequestParam("couponId") String couponId) {
//        if (ObjectUtil.isNull(user))
//            return tokenOverdue();
//        CouponOrderDetailsResponse response = couponOrderService.getUseIndex(user.getId(), couponId);
//        response.setNickname(user.getNickname());
//        return success(response);
//    }
}
